﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media.Imaging;

namespace MVVMtutorialAdvanced.Converter
{
	public class CountryIdToImageConverter : System.Windows.Data.IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if ((int)value <= 0)
				return null;

			var imgPath1 = string.Format("/MVVMtutorialAdvanced;component/Images/country{0}.png", value);
			var streamResourceInfo = Application.GetResourceStream(new Uri(imgPath1, UriKind.Relative));

			var bitmapImage = new BitmapImage();
			bitmapImage.SetSource(streamResourceInfo.Stream);

			return bitmapImage;

			//var uri = HtmlPage.Document.DocumentUri.ToString();
			//var uriRoot = uri.Remove(uri.LastIndexOf('/'), uri.Length - uri.LastIndexOf('/'));

			//var imgPath = string.Format("{0}/images/country{1}.png", uriRoot, value);
			//return new BitmapImage(new Uri(imgPath));
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
